package com.doxiaopu.students.organization.service.impl;

import com.doxiaopu.students.organization.entity.OrderRevenueSettings;
import com.doxiaopu.students.organization.mapper.OrderRevenueSettingsMapper;
import com.doxiaopu.students.organization.service.IOrderRevenueSettingsService;
import com.doxiaopu.students.user.entity.User;
import com.doxiaopu.students.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Primary
public class OrderRevenueSettingsServiceImpl implements IOrderRevenueSettingsService {
    @Autowired
    private OrderRevenueSettingsMapper orderRevenueSettingsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer addInfo(OrderRevenueSettings orderRevenueSettings,Integer id) {
        User user = userMapper.getById(id);
        Date now = new Date();
        orderRevenueSettings.setCreatedUser(user.getUsername());
        orderRevenueSettings.setCreatedTime(now);
        orderRevenueSettings.setModifiedUser(user.getUsername());
        orderRevenueSettings.setModifiedTime(now);
        String str = Long.toString(System.currentTimeMillis());
        orderRevenueSettings.setProfitDistributionId(str);
        return addOrderRevenueSettings(orderRevenueSettings);
    }

    @Override
    public Integer updateInfo(OrderRevenueSettings orderRevenueSettings,Integer id) {
        User user = userMapper.getById(id);
        Date now = new Date();
        orderRevenueSettings.setModifiedUser(user.getUsername());
        orderRevenueSettings.setModifiedTime(now);
        return updateOrderRevenueSettings(orderRevenueSettings);
    }

    @Override
    public OrderRevenueSettings getInfo() {
        return selectInfo();
    }

    /**
     * 增加社联与社团的返利比例
     * @param orderRevenueSettings
     * @return
     */
    private Integer addOrderRevenueSettings(OrderRevenueSettings orderRevenueSettings){
        return orderRevenueSettingsMapper.addOrderRevenueSettings(orderRevenueSettings);
    };

    /**
     * 编辑社联与社团的返利比例
     * @param orderRevenueSettings
     * @return
     */
    private Integer updateOrderRevenueSettings(OrderRevenueSettings orderRevenueSettings){
        return orderRevenueSettingsMapper.updateOrderRevenueSettings(orderRevenueSettings);
    };

    /**
     * 查询信息
     * @param
     * @return
     */
    private OrderRevenueSettings selectInfo(){
        return orderRevenueSettingsMapper.selectInfo();
    };
}
